package b3.widget.tab
{
	import b3.util.*;
	
	import com.greensock.*;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.*;
	import flash.text.*;
	import flash.ui.Mouse;

	public class TabPageNumArea extends Sprite
	{
		private var pageNums:Array;
		private var tab:Tab;
		private var currentPageNumColor:uint;
		private var pageNumColor:uint;
		private var oldPosX:Number;
		private var oldPosY:Number;
		
		public function TabPageNumArea(tab:Tab, pageNumColor:uint = 0x000000, currentPageNumColor:uint = 0xFFFF00)
		{
			super();
			this.tab = tab;
			this.pageNumColor = pageNumColor;
			this.currentPageNumColor = currentPageNumColor;
			pageNums = new Array();
			//页数列表
			for (var i:int = 0; i < tab.totalPageNum; i++)
			{   
				var pageIndexText:TextField = new TextField();
				pageIndexText.selectable = false;
	        	pageIndexText.autoSize = TextFieldAutoSize.LEFT;
				pageIndexText.defaultTextFormat = new TextFormat("微软雅黑", 18, pageNumColor, true);
				if (i == tab.currentPageIndex)
				{
					pageIndexText.textColor = 0xFFFFFF;
					pageIndexText.filters = [new GlowFilter(currentPageNumColor, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
				} 
				pageIndexText.text = (i + 1).toString()
				if (i > 0)
					pageIndexText.x = pageNums[i - 1].x + pageNums[i - 1].width + 2;
				pageIndexText.y = 0;
				addChild(pageIndexText);
				pageNums.push(pageIndexText);
				pageIndexText.addEventListener(MouseEvent.CLICK, onMouseClick);
				pageIndexText.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
				pageIndexText.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			}
		}
		
		public function refreshCurrentPageIndex(pageIndex:int):void
		{
			if (this.visible)
			{
				pageNums[tab.currentPageIndex].textColor = pageNumColor;
				pageNums[tab.currentPageIndex].filters = null;
				pageNums[pageIndex].textColor = 0xFFFFFF;
				pageNums[pageIndex].filters = [new GlowFilter(currentPageNumColor, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			}
		}
		
		private function onMouseClick(event:MouseEvent):void
		{
			var pageIndex:int = parseInt((event.target as TextField).text) - 1;
			if (pageIndex != tab.currentPageIndex)
			{
				refreshCurrentPageIndex(pageIndex);
				tab.currentPageIndex = pageIndex;
			}
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			var scale:Number = 1.2;
			this.oldPosX = event.target.x;
			this.oldPosY = event.target.y;
			TweenLite.to(event.target, 0, {scaleX:scale, scaleY:scale, x:event.target.x - (scale - 1) * event.target.width / 2, y:event.target.y - (scale - 1) * event.target.height});
			Mouse.cursor = "button";
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			TweenLite.to(event.target, 0, {scaleX:1, scaleY:1, x:oldPosX, y:oldPosY});
			Mouse.cursor = "auto";
		}
	}
}